package com.hifar.test.collectdata.pojo;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.alibaba.excel.util.DateUtils;

import lombok.Data;

@Data
public class IntervalBean {

	public Date startTime;
	public Date endTime;
	public List<Date> dataList=new ArrayList<Date>();
	public int pageSize;
	public Long total;
	public Long pageNo;
	public Long pageTotal;
	public double interalValue;
	public static final long HOUR_LONG = 3600000;
	public static final long MINUTE_LONG = 60000;
	public static final long SECOND_LONG = 1000;
	
	public void calculate() {
		long startLong =  startTime.getTime();
		long endLong = endTime.getTime();
		Long totalValue = (long)  Math.floor( (endLong-startLong)/(MINUTE_LONG*interalValue))+1 ;
		this.total = totalValue;
		Long pageTotalValue = (long) Math.ceil(this.total/(double)this.pageSize);
		this.pageTotal = pageTotalValue;
		
		int intervalSecondValue=(int)  Math.floor(MINUTE_LONG*interalValue);
		long startIndex=(int)pageSize*(pageNo-1);
		int indexVaue=0;
		Date dateValue=null;
		List<Date> dateListValue=new ArrayList<Date>();
		for(long i=startIndex;  i<totalValue && i<startIndex+pageSize;i++) {
			dateValue=new Date(startLong+i*intervalSecondValue);
			dateListValue.add(dateValue);
		}
		this.dataList = dateListValue;
		
	}

	@Override
	public String toString() {
		return "IntervalBean [startTime=" + startTime + ", endTime=" + endTime + ", dataList=" + getDataList(dataList)
				+ ", pageSize=" + pageSize + ", total=" + total + ", pageNo=" + pageNo + ", pageTotal=" + pageTotal
				+ ", interalValue=" + interalValue + "]";
	}
	
	public String getDataList(List<Date> dateList) {
		StringBuffer sb=new StringBuffer();
		for(Date date:dateList) {
			sb.append(DateUtils.format(date)+",");
		}
		return sb.toString();
	}
}
